package saci.util;

/**
 *
 * @author Thiago
 */
public class StringUtil {

    public static String xmlFilter(String value) {
        if (value == null) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < value.length(); i++) {
            switch (value.charAt(i)) {
                case '<':
                    result.append("&lt;");
                    break;
                case '>':
                    result.append("&gt;");
                    break;
                case '&':
                    result.append("&amp;");
                    break;
                case '"':
                    result.append("&quot;");
                    break;
                case '\'':
                    result.append("&#39;");
                    break;
                default:
                    result.append(value.charAt(i));
            }
        }
        return result.toString();
    }

    public static String leftPad(String originalText, int length, char fillChar) {
        StringBuilder sb = new StringBuilder();
        length = length - originalText.length();
        for (int i = 0; i < length; i++) {
            sb.append(fillChar);
        }
        sb.append(originalText);
        return sb.toString();
    }

    public static String rightPad(String originalText, int length, char fillChar) {
        StringBuilder sb = new StringBuilder();
        length = length - originalText.length();
        sb.append(originalText);
        for (int i = 0; i < length; i++) {
            sb.append(fillChar);
        }
        return sb.toString();
    }
    
}
